package leetcode_900;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@author 周杨
 *SubdomainVisitCount_811 计算一个域名及其子域名被访问次数
 *describe:用字符分割 AC 50%
 *2018年10月27日 下午1:04:52
 */
public class SubdomainVisitCount_811 {
	public static void main(String[] args) {
		SubdomainVisitCount_811 test=new SubdomainVisitCount_811();
		test.subdomainVisits(new String[] {"9001 discuss.leetcode.com"});
	}
	
	public List<String> subdomainVisits(String[] cpdomains) {
        Map<String,Integer>map=new HashMap<String,Integer>();
        for(String cpdomain:cpdomains) {
        	String[] pat=cpdomain.split(" ");
        	int count=Integer.parseInt(pat[0]);
        	String[] net=pat[1].split("\\.");
        	StringBuilder sb=new StringBuilder();
        	for(int i=net.length-1;i>=0;--i) {
        		sb.insert(0, net[i]);
        		String now=sb.toString();
        		map.put(now, map.getOrDefault(now, 0)+count);
        		sb.insert(0, ".");
        	}
        }
        List<String> res=new ArrayList<String>();
        for(Map.Entry<String, Integer> entry:map.entrySet()) {
        	StringBuilder sb=new StringBuilder();
        	sb.append(entry.getValue()+" "+entry.getKey());
        	res.add(sb.toString());
        }
        return res;
    }
}
